package com.sofwin.service.impl.contents;

import com.sofwin.dto.FinancialDto;
import com.sofwin.mapper.CDebtMapper;
import com.sofwin.mapper.CTaxationMapper;
import com.sofwin.mapper.CTaxationdebtMapper;
import com.sofwin.pojo.*;
import com.sofwin.service.contents.FinancialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: ssm-parent
 * @description: 纳税、借款、担保及或有负债service层
 * @author: Mr.Xq
 * @create: 2021-04-27 10:21
 * @version:
 **/
@Service
public class FinancialServiceImpl implements FinancialService {

    @Autowired
    private CTaxationdebtMapper taxdebtMapper;
    @Autowired
    private CTaxationMapper taxationMapper;
    @Autowired
    private CDebtMapper debtMapper;


    /**
    * @Description:  根据公司id查询纳税、借款、担保及或有负债表及所有详细信息
    * @Param: * @param companyId:  公司id
    * @return: * @return: com.sofwin.dto.FinancialDto
    * @Author: Mr.Xq
    * @Date: 2021/4/27 0027
    */
    @Override
    public FinancialDto selectFinancialsByCompanyId(Integer companyId) {
        FinancialDto dto = new FinancialDto();
        // 根据公司id查询纳税、借款、担保及或有负债表
        CTaxationdebtWithBLOBs blobs = selectCTaxationdebtWithBLOBs(companyId);
        // 查询结果对应的表id
        Integer taxdebtId = blobs.getId();
        // 根据主表id查询主要税种及税率表
        List<CTaxation> taxations = selectCTaxations(taxdebtId);
        // 根据主表id查询借款余额表
        List<CDebt> debts = selectCDebts(taxdebtId);
        // 将所有查询结果封装到dto并返回
        dto.setTdBlobs(blobs);
        dto.setTaxations(taxations);
        dto.setDebts(debts);
        return dto;
    }

    /**
    * @Description:  根据主表id查询借款余额表
    * @Param: * @param taxdebtId:  主表id
    * @return: * @return: java.util.List<com.sofwin.pojo.CDebt>
    * @Author: Mr.Xq
    * @Date: 2021/4/27 0027
    */
    private List<CDebt> selectCDebts(Integer taxdebtId) {
        CDebtExample exam = new CDebtExample();
        CDebtExample.Criteria criteria = exam.createCriteria();
        criteria.andTaxdebtIdEqualTo(taxdebtId);
        return debtMapper.selectByExample(exam);
    }

    /**
    * @Description:  根据主表id查询主要税种及税率表
    * @Param: * @param taxdebtId:  主表id
    * @return: * @return: java.util.List<com.sofwin.pojo.CTaxation>
    * @Author: Mr.Xq
    * @Date: 2021/4/27 0027
    */
    private List<CTaxation> selectCTaxations(Integer taxdebtId) {
        CTaxationExample exam = new CTaxationExample();
        CTaxationExample.Criteria criteria = exam.createCriteria();
        criteria.andTaxdebtIdEqualTo(taxdebtId);
        return taxationMapper.selectByExample(exam);
    }

    /**
    * @Description:  根据公司id查询纳税、借款、担保及或有负债表
    * @Param: * @param companyId:  公司id
    * @return: * @return: com.sofwin.pojo.CTaxationdebtWithBLOBs
    * @Author: Mr.Xq
    * @Date: 2021/4/27 0027
    */
    private CTaxationdebtWithBLOBs selectCTaxationdebtWithBLOBs(Integer companyId) {
        CTaxationdebtExample exam = new CTaxationdebtExample();
        CTaxationdebtExample.Criteria criteria = exam.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        List<CTaxationdebtWithBLOBs> cTaxationdebtWithBLOBs = taxdebtMapper.selectByExampleWithBLOBs(exam);
        return cTaxationdebtWithBLOBs.get(0);
    }
}
